/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2024. All rights reserved.
 * Description: adapter层重构，rts接口
 * Author: ligang
 * Create: 2022-04-09
 */

#ifndef HCCL_INC_ADAPTER_RTS_H
#define HCCL_INC_ADAPTER_RTS_H

#include <hccl/hccl_types.h>

#include "adapter_rts_common.h"
#include "runtime/rt.h"
#include "hccl/base.h"
#include "runtime/kernel.h"
#include "private_types.h"
#include "runtime/rt_error_codes.h"
#if T_DESC("test", true)
#if T_DESC("Device管理", true)

using rtGetPairPhyDevicesInfoPtr = rtError_t(*)(uint32_t, uint32_t, int32_t, int64_t *);

HcclResult hrtCtxCreate(rtContext_t *createCtx, uint32_t flags, int32_t devId);
HcclResult hrtCtxDestroy(rtContext_t destroyCtx);
HcclResult hrtDeviceGetBareTgid(u32 *pid);

HcclResult hrtGetPairDevicesInfo(u32 phyDevId, u32 otherPhyDevId, s32 infoType, s64 *pValue);
HcclResult hrtGetPairPhyDevicesInfo(u32 phyDevId, u32 otherPhyDevId, s32 infoType, s64 *pValue, rtGetPairPhyDevicesInfoPtr funcPtr);
HcclResult hrtGetPairDeviceLinkTypeRaw(u32 phyDevId, u32 otherPhyDevId, s32 infoType, s64 *pValue);
HcclResult hrtGetPhyDeviceInfo(u32 devicePhysicId, s32 moduleType, s32 infoType, s64 &value);
HcclResult hrtGetVisibleDeviceIdByLogicDeviceId(const int32_t logicDeviceId, int32_t *visibleDeviceId);
#ifdef __cplusplus
extern "C" {
#endif
HcclResult hrtGetDeviceTypeBySocVersion(std::string &socVersion, DevType &devType);
HcclResult hrtMemSyncCopyEx(void *dst, uint64_t destMax, const void *src, uint64_t count, HcclRtMemcpyKind kind);

#ifdef __cplusplus
}  // extern "C"
#endif
constexpr u32 CHIP_VERSION_MAX_LEN = 32;
constexpr s32 HCCL_EXEC_TIME_OUT_OFFSET_S = 5; // 避免与notifywait timeout时间冲突，增加5s的偏移值

HcclResult hrtGetSocVer(s8 *chipVer, const u32 size);

HcclResult hrtGetDeviceMode(rtDeviceMode *deviceMode);

HcclResult stubSetDevice(u32 deviceLogicId);
#endif

#if T_DESC("DeviceMemory管理", true)
rtMemcpyKind_t TranslateMemcpyKind(HcclRtMemcpyKind kind);
HcclResult hrtMemAsyncCopyWithCfg(void *dst, uint64_t destMax, const void *src, uint64_t count,
    HcclRtMemcpyKind kind, rtStream_t stream, uint32_t qosCfg);
HcclResult hrtMemAsyncCopyWithoutCheckKind(void *dst, uint64_t destMax, const void *src, uint64_t count,
    HcclRtMemcpyKind kind, rtStream_t stream);
HcclResult hrtGetPointAttr(HcclRtPointAttr ptrAttr, const void *ptr);
HcclResult hrtIpcSetMemoryName(const void *ptr, u8 *name, u64 ptrMaxLen = INVALID_U64,
    u32 nameMaxLen = INVALID_UINT);
HcclResult hrtIpcDestroyMemoryName(const u8 *name);
HcclResult hrtIpcOpenMemory(void **ptr, const u8 *name);
HcclResult hrtIpcSetMemoryPid(const u8 *name, int pid[], int num);
// 该接口仅用于910_93超节点模式
HcclResult hrtSetIpcMemorySuperPodPid(const u8 *name, s32 peerSdid, s32 peerPid[], s32 pidNum);
HcclResult hrtIpcCloseMemory(const void *ptr);
HcclResult hrtDevMemAlignWithPage(void* &ptr, u64 &size);
HcclResult hrtMemPrefetchToDevice(void *ptr, uint64_t size, int32_t deviceId);
#endif

#if T_DESC("host memory管理", true)
HcclResult hrtMallocHost(void **hostPtr, u64 size);
HcclResult hrtFreeHost(void *hostPtr);

HcclResult HrtDevFree(void *devPtr);
HcclResult HrtDevMalloc(void **devPtr, u64 size);

#endif

#if T_DESC("stream管理", true)
HcclResult hrtSwitchStreamEx(void *devMemPtr1, rtCondition_t condition, void *devMemPtr2,
    HcclRtStream true_stream, HcclRtStream stream, rtSwitchDataType_t dataType);

HcclResult hrtStreamDestroy(rtStream_t stream);
HcclResult hrtStreamCreate(rtStream_t *stream, int32_t priority);
HcclResult hrtStreamCreateWithFlags(rtStream_t *stream, int32_t priority, uint32_t flags);
s32 GetMsTimeFromExecTimeout();
#endif

#if T_DESC("event 同步机制", true)
HcclResult hrtEventCreate(HcclRtEvent *event);
HcclResult hrtEventRecord(HcclRtEvent event, HcclRtStream stream);
HcclResult hrtStreamWaitEvent(HcclRtStream stream, HcclRtEvent event);
HcclResult hrtEventQuery(HcclRtEvent event);

HcclResult hrtGetNotifySize(u32 &notifySize);
HcclResult hrtNotifyGetOffset(HcclRtNotify notify, u64 &offset);
HcclResult hrtNotifyWaitWithTimeOut(rtNotify_t notify, rtStream_t stream, uint32_t timeOut);
HcclResult hrtNotifyWait(rtNotify_t notify, rtStream_t stream);
HcclResult hrtNotifyRecord(rtNotify_t notify, rtStream_t stream);
HcclResult hrtNotifyDestroy(rtNotify_t notify);
HcclResult hrtNotifyCreate(s32 deviceId, rtNotify_t *notify);
HcclResult hrtNotifyGetPhyInfoExt(rtNotify_t notify, rtNotifyPhyInfo *notifyInfo);
#endif

#if T_DESC("EnableP2P", true)
HcclResult hrtEnableP2P(u32 deviceLogicId, u32 devicePhyId);
HcclResult hrtDisableP2P(u32 deviceLogicId, u32 devicePhyId);
HcclResult hrtGetP2PStatus(u32 deviceLogicId, u32 devicePhyId, u32 *status);
#endif
#if T_DESC("RtsTaskCallBack", true)
HcclResult hrtUnSubscribeReport(u64 threadId, rtStream_t &stream);
#endif

HcclResult hrtSetIpcNotifyPid(const u8 *name, int32_t pid[], int num);
// 该接口仅用于910_93超节点模式
HcclResult hrtSetIpcNotifySuperPodPid(const u8 *name, s32 peerSdid, s32 peerPid[], s32 pidNum);
HcclResult hrtIpcOpenNotify(rtNotify_t* notify, const u8 *name);
HcclResult hrtReduceAsync(void* dst, uint64_t destMax, const void* src, uint64_t count, rtRecudeKind_t kind,
    rtDataType_t type, rtStream_t stream);
HcclResult hrtReduceAsyncWithCfg(void *dst, uint64_t destMax, const void *src, uint64_t count, rtRecudeKind_t kind,
    rtDataType_t type, rtStream_t stream, uint32_t qosCfg);
HcclResult hrtIpcIntNotice(const rtIpcIntNoticeInfo_t * const Info, rtStream_t stm);
HcclResult hrtReduceAsyncV2(void* dst, uint64_t destMax, const void* src, uint64_t count, rtRecudeKind_t kind,
    rtDataType_t type, rtStream_t stream, void *overflowAddr);
HcclResult hrtRDMASend(u32 qpn, u32 wqe_index, rtStream_t stream);
HcclResult hrtCallbackLaunch(rtCallback_t callBackFunc, void *fnData, rtStream_t stream, bool isBlock);
HcclResult hrtIpcSetNotifyName(rtNotify_t notify, u8* name, uint32_t len);
HcclResult hrtDevBinaryRegister(const rtDevBinary_t *bin, BinHandle *handle);
HcclResult hrtMemcpy(void *dst, uint64_t destMax, const void *src, uint64_t count, HcclRtMemcpyKind kind);
HcclResult hrtKernelLaunch(const void *stubFunc, uint32_t blockDim, void *args, uint32_t argsSize,
    rtSmDesc_t *smDesc, rtStream_t stream);
HcclResult hrtKernelLaunchWithFlagV2(const void *stubFunc, uint32_t blockDim, rtArgsEx_t *argsInfo, rtSmDesc_t *smDesc,
    rtStream_t stream, uint32_t flags, const rtTaskCfgInfo_t *cfgInfo);
HcclResult hrtFunctionRegister(BinHandle binHandle, const void *stubFunc, const char *stubName, const void *devFunc,
    uint32_t funcMode);
HcclResult hrtRDMADBSend(uint32_t dbindex, uint64_t dbinfo, rtStream_t stream);
HcclResult hrtDevBinaryUnRegister(BinHandle handle);
HcclResult hrtGetIsHeterogenous(u32 &heterogenous);
HcclResult hrtSetDeviceSatMode(rtFloatOverflowMode_t floatOverflowMode);
HcclResult hrtAicpuKernelLaunch(const rtKernelLaunchNames_t &launchNames,
    uint32_t blockDim, const void *args, uint32_t argsSize, rtSmDesc_t *smDesc, rtStream_t stm);
HcclResult hrtFftsPlusTaskLaunchWithFlag(rtFftsPlusTaskInfo_t *fftsPlusTaskInfo, rtStream_t stm, u32 flag);

HcclResult hrtNotifyGetAddr(rtNotify_t signal, u64 *notifyAddr);

HcclResult hrtGetRdmaDoorbellAddr(u32 dbIndex, u64 &dbAddr);
HcclResult hrtKernelGetAddrAndPrefCnt(void *hdl, const uint64_t tilingKey, const void * const stubFunc,
    const uint32_t flag, void **addr, uint32_t *prefetchCnt);
HcclResult hrtGetDevArgsAddr(rtStream_t stm, rtArgsEx_t *argsInfo, void **devArgsAddr, void **argsHandle);
HcclResult hrtNotifyCreateWithFlag(int32_t deviceId, rtNotify_t *notify);
HcclResult hrtIpcOpenNotifyWithFlag(rtNotify_t *notify, const u8 *name, uint32_t flags);

HcclResult hrtStreamGetSqid(const rtStream_t stm, uint32_t *sqId);
HcclResult hrtStreamGetCqid(const rtStream_t stm, uint32_t *cqId, uint32_t *logicCqId);
HcclResult hrtTaskAbortHandleCallback(rtTaskAbortCallBack callback, void *args);
HcclResult hrtResourceClean(int32_t devId, rtIdType_t type);
HcclResult hrtGetHccsPortNum(u32 deviceLogicId, s32 &num);
#endif
#endif
